package fr.dlachiche.gtasksync.network;

import java.io.IOException;

import com.google.api.client.util.DateTime;
import com.google.api.services.tasks.Tasks.TasksOperations.Move;
import com.google.api.services.tasks.model.Task;
import com.google.api.services.tasks.model.Tasks;

import fr.dlachiche.gtasksync.auth.TasksFactory;

/**
 * Manage tasks between GTask and application, using google service
 * 
 * @author dlachiche
 *
 */
public class TaskNetwork {
	
	/**
	 * Retreive user's tasks in the specified task list.
	 * If no task list is specified, the \@default task list is used.
	 * @param taskListId ID of the task list
	 * @return The tasks in the specified task list
	 * @throws IOException If an error append during retreiving tasks
	 */
	public static Tasks retreiveTasks(String taskListId) throws IOException {
		if (taskListId == null || taskListId.equals("")) {
			taskListId = "@default";
		}
		Tasks tasks = TasksFactory.getService().tasks().list(taskListId).execute();
		return tasks;
	}
	
	/**
	 * Retreive a single task identified by a task id, in the specified task list.
	 * If no task list is specified, the \@default task list is used.
	 * @param taskListId ID of the task list
	 * @param taskId ID of the task
	 * @return The single task
	 * @throws IOException If an error append during network connection
	 */
	public static Task retreiveTask(String taskListId, String taskId) throws IOException {
		if (taskListId == null || taskListId.equals("")) {
			taskListId = "@default";
		}
		if (taskId == null || taskId.equals("")) {
			return null;
		}
		Task task = TasksFactory.getService().tasks().get("@default", "taskID").execute();
		return task;
	}

	/**
	 * Create a new task and insert it in the specified task list.
	 * If no task list specified, the \@default task list is used.
	 * @param taskListId ID of the task list
	 * @param taskTitle Title for the task
	 * @param taskNotes Notes for the task
	 * @param taskDue Due date for the task
	 * @return The new created task
	 * @throws IOException If an error append during network connection
	 */
	public static Task createTask(String taskListId, String taskTitle, String taskNotes, 
			DateTime taskDue) throws IOException {
		
		if (taskListId == null || taskListId.equals("")) {
			taskListId = "@default";
		}
		
		Task task = new Task();
		task.setTitle(taskTitle);
		task.setNotes(taskNotes);
		task.setDue(taskDue);

		Task result = TasksFactory.getService().tasks().insert(taskListId, task).execute();
		return result;
	}

	public static void updateTask() throws IOException {
		// First retrieve the task to update.
		Task task = TasksFactory.getService().tasks().get("@default", "taskID").execute();
		task.setStatus("completed");

		Task result = TasksFactory.getService().tasks().update("@default", task.getId(), task).execute();
		// Print the completed date.
		System.out.println(result.getCompleted());
	}
	
	public static void orderTask() throws IOException {
		Move move = TasksFactory.getService().tasks().move("@default", "taskID");
		move.setParent("parentTaskID");
		move.setPrevious("previousTaskID");
		Task result = move.execute();

		// Print the new values.
		System.out.println(result.getParent());
		System.out.println(result.getPosition());
	}
	
	/**
	 * Delete the specified task in the specified task list.
	 * If no task list is specified, the \@default task list is used.
	 * @param taskListId ID of the task list
	 * @param taskId ID of the task
	 * @throws IOException If an error append during network connection
	 */
	public static void deleteTask(String taskListId, String taskId) throws IOException {
		if (taskListId == null || taskListId.equals("")) {
			taskListId = "@default";
		}
		if (taskId == null || taskId.equals("")) {
			TasksFactory.getService().tasks().delete(taskListId, taskId).execute();
		}
	}
}
